//
//  HPYComposeBottomView.swift
//  SwiftVVBo
//
//  Created by hepeiyong on 16/8/18.
//  Copyright © 2016年 hpyhn. All rights reserved.
//

import UIKit
// 底部视图按钮的枚举
enum HPYComposeBottomViewType: Int {
    //图片
    case Picture = 0
    // @
    case Mention = 1
    // #
    case Trend = 2
    // 笑脸
    case Emoticon = 3
    // +
    case Add = 4
}

class HPYComposeBottomView: UIView {

    var emoticonButton: UIButton?

    // 声明一个闭包 01
    var closure:((type: HPYComposeBottomViewType)->())?
    // 定义一个属性 供外界设置 (是否是表情键盘)
    var isEmoticon: Bool = false{
        didSet{
            /*
             true  代表当前为 代表显示为表情键盘  -> 显示为 键盘图片
             false 代表当前为 代表显示为系统键盘  -> 显示为 笑脸图片
             */
            // true  代表当前为 代表显示为表情键盘  -> 显示为 键盘图片//
            let imgName: String
            if isEmoticon {
                imgName = "compose_keyboardbutton_background"
            }else {
                // false 代表当前为 代表显示为系统键盘  -> 显示为 笑脸图片
                imgName = "compose_emoticonbutton_background"
            }

            // 设置图片
            emoticonButton?.setImage(UIImage(named: "\(imgName)"), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 监听事件
    func buttonClick(button: UIButton) {
        print(button.tag)
        // 03 执行闭包 // HPYComposeBottomViewType(rawValue: button.tag) int 转枚举名
        closure?(type: HPYComposeBottomViewType(rawValue: button.tag)!)
    }

    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)

        // 添加按钮
        let pictureButton = addChildButtons("compose_toolbar_picture",type: .Picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background",type: .Trend)
        emoticonButton = addChildButtons("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildButtons("compose_add_background",type: .Add)
        // 添加约束

        pictureButton.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }

        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }

        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }

        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }

        addButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
    }


    func addChildButtons(imgName: String, type: HPYComposeBottomViewType) -> UIButton{
        let button = UIButton()
        // 设置tag //type.rawValue 通过枚举名 得到对应的枚举值
        button.tag = type.rawValue
        // 添加事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }
    
}
